package com.uetoken.cn.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.model.PageNumBottonModel;
import com.uetoken.cn.manager.UEDataManager;

import java.util.ArrayList;
import java.util.List;

public class PageNumBottonAdapter extends RecyclerView.Adapter<PageNumBottonAdapter.MyViewHolder> {

    private Context mContext;
    private List<PageNumBottonModel> mPageNumBottonList;
    private OnItemClickListener mClickListener;


    public PageNumBottonAdapter(Context context, List<PageNumBottonModel> pageNumBottonModelList) {
        this.mContext = context;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_page_num, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        PageNumBottonModel mPageNumBottonModel = mPageNumBottonList.get(position);
        holder.mPageNumBtn.setText(mPageNumBottonModel.getPageNum() + "");
        boolean isPage = mPageNumBottonModel.isPage();

        if (isPage) {
            holder.mPageNumBtn.setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_app_theme));
        } else {
            holder.mPageNumBtn.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorFA));
        }

        if (mClickListener != null) {
            //绑定点击事件
            holder.mPageNumLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //把条目的位置回调回去
                    mClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        mPageNumBottonList = UEDataManager.getInstance().getPageSumList();
        return mPageNumBottonList == null ? 0 : mPageNumBottonList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView mPageNumBtn;
        private RelativeLayout mPageNumLayout;

        public MyViewHolder(View itemView) {
            super(itemView);
            mPageNumBtn = (TextView) itemView.findViewById(R.id.pageNumBtn);
            mPageNumLayout = itemView.findViewById(R.id.pageNumLayout);
        }
    }


    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }
}
